package NotifyTest;

public class BaoZiPu extends Thread {
    private BaoZi bz;//1.创建包子变量

    public BaoZiPu(BaoZi bz) {
        this.bz = bz;
    }

    @Override
    public void run() {//设置线程任务 生产包子
        int count = 0;
        while (true) {
            synchronized (bz) {
                if (bz.flag == true) {
                    try {
                        bz.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (count % 2 == 0) {
                    bz.pi = "薄皮";
                    bz.xian = "三鲜";
                } else {
                    bz.pi = "冰皮";
                    bz.xian = "牛肉大葱";
                }
                count++;
                System.out.println("包子铺正在生产:" + bz.pi + bz.xian + "包子，吃货还在等待中");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                bz.flag = true;//生产好包子，修改包子的状态是true
                bz.notify();//唤醒吃货线程，开始吃包子
                System.out.println("包子铺已经生成好了:" + bz.pi + bz.xian + "包子，吃货可以开始吃了");
            }
        }
    }
}